package cn.xscrum.vtsp.controller;

import cn.xscrum.common.annotation.Log;
import cn.xscrum.common.core.controller.BaseController;
import cn.xscrum.common.core.domain.AjaxResult;
import cn.xscrum.common.core.page.TableDataInfo;
import cn.xscrum.common.enums.BusinessType;
import cn.xscrum.common.utils.poi.ExcelUtil;
import cn.xscrum.vtsp.domain.*;
import cn.xscrum.vtsp.service.*;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * 志愿者黑名单Controller
 *
 * @author liuwl
 * @date 2021-10-01
 */
@Controller
@RequestMapping("/vtsp/blacklist")
public class VtspBlacklistController extends BaseController
{
    //template资源前缀
    private String prefix = "vtsp/blacklist";

    @Autowired
    private IVtspBlacklistService vtspBlacklistService; //定义黑名单志愿者Service接口对象

    @Autowired
    private IVtspAreasService vtspAreasService; //定义国家地区管理Service接口对象

    @Autowired
    private IVtspSectorService vtspSectorService;   //定义部门Service接口对象

    @Autowired
    private IVtspDutiesService vtspDutiesService;   //定义职务Service接口对象

    @Autowired
    private IVtspAcademyService vtspAcademyService; //定义学院Service接口对象

    /**
     * 初始加载的志愿者黑名单信息
     *
     * @return 志愿者黑名单页面
     */
    @RequiresPermissions("team:blacklist:view")
    @GetMapping()
    public String blacklist(Model model)
    {
        //获取部门列表
        List<VtspSector> sectorList = vtspSectorService.selectVtspSectorList(new VtspSector());
        //获取学院列表
        List<VtspAcademy> academyList = vtspAcademyService.selectVtspAcademyList(new VtspAcademy());
        model.addAttribute("sectorList", sectorList);
        model.addAttribute("academyList", academyList);
        return prefix + "/blacklist";
    }

    /**
     * 查询志愿者黑名单列表
     *
     * @param vtspBlacklist 志愿者黑名单信息
     * @return 查询到的志愿者黑名单信息列表
     */
    @RequiresPermissions("team:blacklist:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(VtspBlacklist vtspBlacklist)
    {
        //获取志愿者黑名单列表信息
        List<VtspBlacklist> list = vtspBlacklistService.selectVtspBlacklistList(vtspBlacklist);
        //数据回显
        for(VtspBlacklist blacklist : list)
        {
            if(blacklist != null)
            {
                String sector = blacklist.getSectorName();    //部门
                String duties = blacklist.getDutiesName();    //职务
                String academy = blacklist.getVolunteerAcademy();     //学院
                //利用正则表达式判断duties是否为数字
                Pattern pattern = Pattern.compile("[0-9]+");
                if((pattern.matcher(sector)).matches())        //部门为数字
                {
                    Long sectorId = Long.parseLong(sector);         //String转Long
                    VtspSector vtspSector = vtspSectorService.selectVtspSectorById(sectorId);
                    String sectorName = vtspSector.getSectorName();
                    blacklist.setSectorName(sectorName);        //回显
                }
                if((pattern.matcher(duties)).matches())        //职务为数字
                {
                    Long dutiesId = Long.parseLong(duties);         //String转Long
                    VtspDuties vtspDuties = vtspDutiesService.selectVtspDutiesById(dutiesId);
                    String dutiesName = vtspDuties.getDutiesName();
                    blacklist.setDutiesName(dutiesName);        //回显
                }
                if((pattern.matcher(academy)).matches())        //学院为数字
                {
                    Long academyId = Long.parseLong(academy);         //String转Long
                    VtspAcademy vtspAcademy = vtspAcademyService.selectVtspAcademyById(academyId);
                    String academyName = vtspAcademy.getAcademyName();
                    blacklist.setVolunteerAcademy(academyName);        //回显
                }
            }
        }
        startPage();
        return getDataTable(list);
    }

    /**
     * 修改黑名单志愿者信息
     *
     * @param volunteerId 志愿者信息ID
     * @param mmap ModelMap对象
     * @return 修改页面的数据
     */
    @GetMapping("/edit/{volunteerId}")
    public String edit(@PathVariable("volunteerId") Long volunteerId, ModelMap mmap)
    {
        //根据volunteerId查询需要修改的黑名单志愿者信息
        VtspBlacklist vtspBlacklist = vtspBlacklistService.selectVtspBlacklistById(volunteerId);
        //获取部门列表
        List<VtspSector> sectorList = vtspSectorService.selectVtspSectorList(new VtspSector());
        //获取职务列表
        List<VtspDuties> dutiesList = vtspDutiesService.selectVtspDutiesList(new VtspDuties());
        //获取国家地区管理的数据
        List<VtspAreas> areasList = vtspAreasService.selectVtspAreasList(new VtspAreas());
        //获取学院列表
        List<VtspAcademy> academyList = vtspAcademyService.selectVtspAcademyList(new VtspAcademy());
        //获取vtsp_areas的国家信息，即parent_id = 0；若parent_id != 0，则为城市信息
        List<VtspAreas> countryList = new ArrayList<VtspAreas>();        //国家
        List<VtspAreas> cityList = new ArrayList<VtspAreas>();           //省市
        for(VtspAreas vtspAreas : areasList)
        {
            if(vtspAreas != null)
            {
                if(vtspAreas.getParentId() == 0)    //国家
                {
                    countryList.add(vtspAreas);
                }
                else
                {
                    cityList.add(vtspAreas);
                }
            }
        }
        //获取adviserId的volunteer_areaid，由此获取vtsp_areas信息
        Long areasId = vtspBlacklist.getVolunteerAreaid();
        VtspAreas vtspAreas = vtspAreasService.selectVtspAreasById(areasId);
        //展示修改页面的数据
        mmap.put("vtspBlacklist", vtspBlacklist);
        mmap.put("countryList", countryList);
        mmap.put("cityList", JSON.toJSONString(cityList));
        mmap.put("vtspAreas",JSON.toJSONString(vtspAreas));
        mmap.put("sectorList", sectorList);
        mmap.put("dutiesList", dutiesList);
        mmap.put("academyList", academyList);
        return prefix + "/edit";
    }

    /**
     * 修改保存黑名单志愿者
     *
     * @param vtspBlacklist 黑名单志愿者信息
     * @return 修改结果
     */
    @RequiresPermissions("team:blacklist:edit")
    @Log(title = "志愿者黑名单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(VtspBlacklist vtspBlacklist)
    {
        return toAjax(vtspBlacklistService.updateVtspBlacklist(vtspBlacklist));
    }

    /**
     * 导出黑名单志愿者管理列表
     *
     * @param vtspBlacklist 黑名单志愿者信息
     * @return 结果
     */
    @RequiresPermissions("team:blacklist:export")
    @Log(title = "黑名单志愿者管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult exportList(VtspBlacklist vtspBlacklist)
    {
        //获取黑名单志愿者信息
        List<VtspBlacklist> list = vtspBlacklistService.selectVtspBlacklistList(vtspBlacklist);
        //创建Excel表格并导出
        ExcelUtil<VtspBlacklist> util = new ExcelUtil<VtspBlacklist>(VtspBlacklist.class);
        return util.exportExcel(list, "blacklist");
    }

    /**
     * 查看黑名单志愿者详情
     *
     * @param volunteerId 志愿者信息ID
     * @param mmap ModelMap对象
     * @return 黑名单志愿者详情列表
     */
    @GetMapping("/detail/{volunteerId}")
    public String detail(@PathVariable("volunteerId") Long volunteerId, ModelMap mmap)
    {
        //根据volunteerId查询黑名单志愿者信息
        VtspBlacklist vtspBlacklist = vtspBlacklistService.selectVtspBlacklistById(volunteerId);
        if(vtspBlacklist != null)
        {
            String sector = vtspBlacklist.getSectorName();    //部门
            String duties = vtspBlacklist.getDutiesName();    //职务
            String academy = vtspBlacklist.getVolunteerAcademy();     //学院
            //利用正则表达式判断duties是否为数字
            Pattern pattern = Pattern.compile("[0-9]+");
            if((pattern.matcher(sector)).matches())        //部门为数字
            {
                Long sectorId = Long.parseLong(sector);         //String转Long
                VtspSector vtspSector = vtspSectorService.selectVtspSectorById(sectorId);
                String sectorName = vtspSector.getSectorName();
                vtspBlacklist.setSectorName(sectorName);        //回显
            }
            if((pattern.matcher(duties)).matches())        //职务为数字
            {
                Long dutiesId = Long.parseLong(duties);         //String转Long
                VtspDuties vtspDuties = vtspDutiesService.selectVtspDutiesById(dutiesId);
                String dutiesName = vtspDuties.getDutiesName();
                vtspBlacklist.setDutiesName(dutiesName);        //回显
            }
            if((pattern.matcher(academy)).matches())        //学院为数字
            {
                Long academyId = Long.parseLong(academy);         //String转Long
                VtspAcademy vtspAcademy = vtspAcademyService.selectVtspAcademyById(academyId);
                String academyName = vtspAcademy.getAcademyName();
                vtspBlacklist.setVolunteerAcademy(academyName);        //回显
            }
        }
        //展示详情页面的数据
        mmap.put("vtspBlacklist", vtspBlacklist);
        return prefix + "/detail";
    }

    /**
     * 查询黑名单志愿者服务记录详情
     *
     * @param volunteerId 志愿者信息ID
     * @param mmap ModelMap对象
     * @return 查询到的黑名单志愿者服务记录
     */
    @PostMapping("/detail/{volunteerId}/serve")
    @ResponseBody
    public TableDataInfo listA(@PathVariable("volunteerId") Long volunteerId, ModelMap mmap)
    {
        mmap.put("volunteerId" ,volunteerId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  //日期格式化
        //获取志愿者服务记录
        List<VtspBlacklist> list = vtspBlacklistService.selectServeList(volunteerId);
        for(VtspBlacklist vtspBlacklist : list)
        {
            if(vtspBlacklist != null)
            {
                //设置活动时间为：活动开始时间 至 活动结束时间
                String activityTime = sdf.format(vtspBlacklist.getActivityBegin()) + " 至 " + sdf.format(vtspBlacklist.getActivityEnd());
                vtspBlacklist.setActivityTime(activityTime);
            }
        }
        startPage();
        return getDataTable(list);
    }

    /**
     * 查询黑名单志愿者报名记录详情
     *
     * @param volunteerId 志愿者信息ID
     * @param mmap ModelMap对象
     * @return 查询到的黑名单志愿者报名记录
     */
    @PostMapping("/detail/{volunteerId}/enroll")
    @ResponseBody
    public TableDataInfo listB(@PathVariable("volunteerId") Long volunteerId, ModelMap mmap)
    {
        mmap.put("volunteerId" ,volunteerId);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");  //日期格式化
        //获取志愿者报名记录
        List<VtspBlacklist> list = vtspBlacklistService.selectEnrollList(volunteerId);
        for(VtspBlacklist vtspBlacklist : list)
        {
            if(vtspBlacklist != null)
            {
                //设置活动时间为：活动开始时间 至 活动结束时间
                String activityTime = sdf.format(vtspBlacklist.getActivityBegin()) + " 至 " + sdf.format(vtspBlacklist.getActivityEnd());
                vtspBlacklist.setActivityTime(activityTime);
            }
        }
        startPage();
        return getDataTable(list);
    }

    /**
     * 移出黑名单 修改志愿者状态为正式志愿者6
     *
     * @param volunteerId 志愿者信息ID
     * @return 结果
     */
    @RequiresPermissions("team:blacklist:move")
    @Log(title = "黑名单志愿者管理", businessType = BusinessType.UPDATE)
    @PostMapping( "/move")
    @ResponseBody
    public AjaxResult removeBlacklist(Long volunteerId)
    {
        return toAjax(vtspBlacklistService.removeVtspBlacklistById(volunteerId));
    }
}
